package com.tjetc.view.manage;

import com.tjetc.domain.*;
import com.tjetc.proxy.ClientProxy;
import com.tjetc.service.*;
import com.tjetc.vo.Result;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Scanner;

public class DoctorManageView {
    private Scanner scanner=new Scanner(System.in);
    private DoctorService doctorService= ClientProxy.getProxyInstance(DoctorService.class);
    private NewBornService newBornService=ClientProxy.getProxyInstance(NewBornService.class);
    private DiseaseService diseaseService=ClientProxy.getProxyInstance(DiseaseService.class);
    private ParentService parentService=ClientProxy.getProxyInstance(ParentService.class);
    private IdCardService idCardService=ClientProxy.getProxyInstance(IdCardService.class);
    public void start() {
        while (true){
            try {
                System.out.println("1.增加新生儿");
                System.out.println("2.删除新生儿");
                System.out.println("3.修改新生儿信息");
                System.out.println("4.查询新生儿信息");
                System.out.println("5.查询新生儿列表");
                System.out.println("6.查询某个姓氏的新生儿");
                System.out.println("7.录入新生儿患病情况");
                System.out.println("8.查看患病新生儿");
                System.out.println("0.返回上一级");
                System.out.println("请输入对应的编号(0~8)");
                int i = scanner.nextInt();
                switch (i){
                    case 1:
                        add();
                        break;
                    case 2:
                        del();
                        break;
                    case 3:
                        upd();
                        break;
                    case 4:
                        sel();
                        break;
                    case 5:
                        list();
                        break;
                    case 6:
                        selBN();
                        break;
                    case 7:
                        health();
                        break;
                    case 8:
                        serch();
                        break;
                    case 0:
                        return;
                }
            }catch (Exception e){
                System.out.println("输入错误,请重新输入");
                scanner.nextLine();
            }
        }
    }

    private void health() {
        System.out.println("请输入新生儿编号");
        int newbornId = scanner.nextInt();
        System.out.println("请输入疾病编号");
        int diseaseId = scanner.nextInt();
        Newborn newborn=newBornService.findNID(newbornId);
        if (newborn==null){
            System.out.println("不存在该新生儿");
        }else {
            Disease disease=diseaseService.findDid(diseaseId);
            if (disease==null){
                System.out.println("不存在该疾病");
            }else {
                Health health=new Health();
                health.setNewbornId(newbornId);
                health.setDiseaseId(diseaseId);
                Result result=newBornService.addH(health);
                System.out.println(result.getMsg());
            }
        }
    }

    private void selBN() {
        System.out.println("请输入新生儿的姓氏");
        String x = scanner.next();
        Result result=newBornService.selBN(x);
        List<Newborn> list= (List) result.getData();
        for (Newborn newborn : list) {
            System.out.println(newborn);
        }
        System.out.println(result.getMsg());
    }

    private void serch() {
        List<Health> list1=doctorService.serch1();
        for (Health health : list1) {
            System.out.println(health);
        }

    }



    private void list() {
        List<Newborn> list=doctorService.list();
        for (Newborn newborn : list) {
            System.out.println(newborn);
        }
    }

    private void sel() {
        System.out.println("请输入需要查询的新生儿的编号");
        int id = scanner.nextInt();
        Newborn newborn=doctorService.sel(id);
        System.out.println(newborn);
    }

    private void upd() {
        list();
        System.out.println("请输入需要修改的新生儿的编号");
        int id = scanner.nextInt();
        Newborn newborn2=newBornService.sel(id);
        if (newborn2==null){
            System.out.println("不存在该新生儿");
        }else {
            System.out.println("请输入修改后新生儿的名字");
            String name = scanner.next();
            System.out.println("请输入修改后新生儿的性别");
            String gender=scanner.next();
            if (!(gender.equals("男"))&&!(gender.equals("女"))){
                System.out.println("输入错误");
                }else {
                System.out.println("请输入父母的编号");
                int parentId=scanner.nextInt();
                Parent parent=parentService.findBI(parentId);
                if (parent==null){
                    System.out.println("不存在该家长");
                }else {
                    System.out.println("请输入修改后新生儿的身高");
                    double height=scanner.nextDouble();
                    System.out.println("请输入修改后新生儿的体重");
                    double weight=scanner.nextDouble();
                    String postCode=null;
                    boolean b = true;
                    while (b) {
                        System.out.println("请输入新生儿邮政编码");
                        postCode = scanner.next();

                        if (postCode.matches("\\d{6}")) {
                            b = false;
                        } else {
                            System.out.println("邮政编码不对，请重新输入");
                        }
                    }
                    System.out.println("请输入对应医护人员的编号");
                    int doctorId = scanner.nextInt();
                    Doctor doctor =doctorService.findId(doctorId);
                    if (doctor==null){
                        System.out.println("该医护人员不存在");
                    }else {
                        Newborn newborn = new Newborn();
                        newborn.setId(id);
                        newborn.setName(name);
                        newborn.setGender(gender);
                        newborn.setBirthDate(new Date());
                        newborn.setBirthHeight(height);
                        newborn.setBirthWeight(weight);
                        newborn.setPostCode(postCode);
                        newborn.setDoctorId(doctorId);
                        newborn.setParentId(parentId);
                        Result result=doctorService.upd(newborn);
                        System.out.println(result.getMsg());
                        idCardService.updateIdCardById(id);
                    }
                }
            }
        }

    }

    private void del() {
        System.out.println("请输入新生儿的编号");
        int id = scanner.nextInt();
        Result result=doctorService.del(id);
        System.out.println(result.getMsg());
    }

    private void add() {
        System.out.println("请输入新生儿的名字");
        String name = scanner.next();
        System.out.println("请输入新生儿的性别");
        String gender=scanner.next();
        if (!(gender.equals("男"))&&!(gender.equals("女"))){
            System.out.println("输入错误");
        }else {
            System.out.println("请输入新生儿的身高");
            double height=scanner.nextDouble();
            System.out.println("请输入新生儿的体重");
            double weight=scanner.nextDouble();
            System.out.println("请输入父母的编号");
            int id=scanner.nextInt();
            Parent parent=parentService.findBI(id);
            if (parent==null){
                System.out.println("不存在该家长");
            }else {
                String postCode = null;
                boolean b = true;
                while (b) {
                    System.out.println("请输入新生儿邮政编码");
                    postCode = scanner.next();

                    if (postCode.matches("\\d{6}")) {
                        b = false;
                    } else {
                        System.out.println("邮政编码不对，请重新输入");
                    }
                }
                System.out.println("请输入对应医护人员的编号");
                int doctorId = scanner.nextInt();
                Doctor doctor = doctorService.findId(doctorId);
                if (doctor == null) {
                    System.out.println("该医护人员不存在");
                } else {
                    Newborn newborn = new Newborn();
                    newborn.setId(id);
                    newborn.setName(name);
                    newborn.setGender(gender);
                    newborn.setBirthDate(new Date());
                    newborn.setBirthHeight(height);
                    newborn.setBirthWeight(weight);
                    newborn.setPostCode(postCode);
                    newborn.setDoctorId(doctorId);
                    newborn.setParentId(id);
                    Result result = doctorService.add(newborn);
                    System.out.println(result.getMsg());
                }
            }
        }
    }
}
